package com.sgj.easypoi.controller;

import com.sgj.easypoi.entity.User;
import com.sgj.easypoi.utils.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Guoji Shen
 * @Date: 2021/7/14 13:31
 */
@RestController
public class ExcelController {
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
        // 存入数据
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            User user = User.builder()
                    .name("小白" + i)
                    .sex((i % 2 == 0) ? 1 : 0)
                    .birthday(new Date())
                    .build();
            list.add(user);
        }

        // 导出
        ExcelUtil.exportExcel(list, "员工", "大大", User.class, "aaa.xls", response);
    }


    @GetMapping("/importExcel")
    public void importExcel() throws Exception {
        // 导入文件路径
        String filePath = "D:\\chrome download\\aaa.xls";
        // 解析excel，
        List<User> userList = ExcelUtil.importExcel(filePath, 1, 1, User.class);
        // 也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共["+userList.size()+"]行");

        // TODO 保存数据库
    }

}